package com.ruoyi.project.cyq.task.postpone.controller;


import com.alibaba.fastjson.JSON;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cyq.task.postpone.domain.PostPone;
import com.ruoyi.project.cyq.task.postpone.domain.PostPoneVo;
import com.ruoyi.project.cyq.task.postpone.service.IPostPoneService;
import com.ruoyi.project.cyq.task.task.domain.Task;
import com.ruoyi.project.cyq.task.task.service.ITaskService;
import com.ruoyi.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/cyq/task/postpone")
public class    PostPoneController  extends BaseController {

    private String perfix = "cyq/task/taskpostpone";


    @Autowired
    private IPostPoneService iPostPoneService;


    @Autowired
    private ITaskService iTaskService;
    /**
     * 跳转到延期申请页面
     * @return
     */
    @RequiresPermissions("cyq:taskpostpone:apply")
    @GetMapping()
    public String operlog(){
        return perfix + "/postpone";
    }

    /**
     * 展示延期申请的页面数据
     * @param postPone
     * @return
     */
    @RequiresPermissions("cyq:task:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PostPone postPone) {
        startPage();
        List<PostPoneVo> list = iPostPoneService.selectPostPoneList(postPone);
        return getDataTable(list);
    }

    /**
     * 展示延期审批的页面数据
     * @param postPone
     * @return
     */
    @RequiresPermissions("cyq:taskpostpone:approval")
    @PostMapping("/list1")
    @ResponseBody
    public TableDataInfo list1(PostPone postPone) {
        startPage();
        List<PostPoneVo> list = iPostPoneService.selectPostPoneList1(postPone);
        System.out.println(list);
        return getDataTable(list);
    }



    @RequiresPermissions("cyq:task:add")
    @GetMapping("/add")
    public String add(ModelMap modelMap){
       List<Task> tasks =  iTaskService.selectAllTaskTitle();
        modelMap.put("taskTitles",tasks);

        return perfix + "/add";
    }


    @ResponseBody
    @RequestMapping("selectUserNameByTaskTitle")
    public String selectUserNameByTaskTitle(String taskTitle){
       User user =  iTaskService.selectUserNameByTaskTitle(taskTitle);
        return JSON.toJSONString(user);
    }


    @RequiresPermissions("cyq:taskpostpone:apply:add")
    @Log(title = "任务管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Task task,String postPoneCause, String taskEndTime) {
        return toAjax( iPostPoneService.insertPostPone(task,taskEndTime,postPoneCause));
    }

    @GetMapping("/approval")
    public String approval(){
        return perfix + "/postponeapprova";
    }



}
